﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Controls.DataVisualization;
using System.Windows.Controls.DataVisualization.Charting;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using System.Windows.Threading;
using BehaviorModel;
using QSDesigner.Windows.Models;

namespace QSDesigner.Windows
{
    /// <summary>
    /// Interaction logic for СhannelsTimeAllocationWindow.xaml
    /// </summary>
    public partial class СhannelsTimeAllocationWindow : Window, IСhannelsTimeAllocationWindow, IUpdatableWindow
    {
        public DispatcherTimer DispatcherTimer { get; set; }

        //public ObservableCollection<ChannelsTimeAllocationModel> Data = new ObservableCollection<ChannelsTimeAllocationModel>();

        public СhannelsTimeAllocationWindow()
        {
            InitializeComponent();

            DispatcherTimer = new DispatcherTimer { Interval = TimeSpan.FromMilliseconds(1000) };
            DispatcherTimer.Tick += (s, a) =>
            {
                Action action = UpdateChart;
                Dispatcher.Invoke(action);
            };
        }

        public void UpdateChart()
        {
            lock (StatisticController.GetInstance().Statistic.Locker)
            {
                var stat = StatisticController.GetInstance().Statistic.ServiceChannelsWorkingDistribution;
                var data = stat.ToDictionary(item => item.Key + " канал(а/ов)", item => item.Value);

                pieChart.DataContext = data;

                /*Data.Clear();

                foreach (var item in stat)
                {
                    Data.Add(new ChannelsTimeAllocationModel { Percent = item.Value, Label = item.Key + " канал(а/ов)", Channel = item.Key }); 
                }

                ((PieSeries) pieChart.Series[0]).ItemsSource = Data;*/

            }
        }

        public void StartFetchData()
        {
            // Start the timer
            DispatcherTimer.Start();
        }

        public void StopFetchData()
        {
            DispatcherTimer.Stop();
        }
       
    }
}
